package model;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DBmanager {
	
	private static DBmanager uniqueInstance=null;
	
	private Connection con = null ;
	private final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
	private final String dbname = "jdbc:derby:/Users/Juan/Documents/School/Semester 3/Webtechnologie/Code/Pokergame/UsersDB" ;
//	private final String dbname = "jdbc:Derby:" + System.getProperty("user.home") + "/Pokergame;create=true" ;
	
	private DBmanager(){
		if(!dbExists(dbname)) {
			System.err.println("De database bestaat niet.") ;			
		}
	}
	
	public static synchronized DBmanager getInstance() {
		if (uniqueInstance == null) {
			uniqueInstance = new DBmanager();
		}
		return uniqueInstance;
	}
	
	private Boolean dbExists(String dataSource) {
		Boolean exists = false ;
		try{
			Class.forName(driver) ;
            con = DriverManager.getConnection(dataSource, "Juan", "pokergame");
			exists = true ;
		} catch(Exception e){
			; // Do nothing, as DB does not (yet) exist
		}
		return(exists) ;
	}

	public void close() {
		try {
			con = DriverManager.getConnection(dbname + ";shutdown=true") ;
		}catch(SQLException se) {
			; // Do Nothing. System has shut down.
		con = null ;
		}
	}
	
	public Connection getConnection() {
		return con;
	}
}
